﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.Bll;
using Telerik.Web.UI;

public partial class Topics_TopicPosts : BaseTopicPage
{
    public override Role.Roles[] AllowedRoles
    {
        get { return new Role.Roles[] { Role.Roles.Developer, Role.Roles.ContentEditor, Role.Roles.ContentViewer, Role.Roles.ContentPublisher }; }
    }

    public Role.Roles[] EditRoles
    {
        get { return new Role.Roles[] { Role.Roles.Developer, Role.Roles.ContentViewer, Role.Roles.ContentPublisher }; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        ClientScript.RegisterStartupScript(this.GetType(), "lnkAddUniqueId", string.Format("var lnkAddUniqueId = \"{0}\";", lnkAdd.UniqueID), true);
        if (!IsPostBack)
        {
            if (CurrentTopic != null)
            {
                this.Title = string.Concat(CurrentTopic.Name, " Posts");
            }
            else if(CurrentGroup != null)
            {
                this.Title = string.Concat(CurrentGroup.Name, " Posts");
            }

            tabSiteVersions.SelectedIndex = 0;
            tabSiteVersions.AppendDataBoundItems = true;
            tabSiteVersions.Tabs.Add(new Telerik.Web.UI.RadTab("Pending", null));
            tabSiteVersions.DataSource = Acms.Bll.SiteVersion.GetAllFuture(AcmsContext.SiteId);
            tabSiteVersions.DataBind();
            tabSiteVersions.Tabs.Add(new Telerik.Web.UI.RadTab("Live", DateTime.Now.ToString()));

            winAdd.NavigateUrl = string.Format("PopUp/SelectPost.aspx?Id={0}", TopicId);

            DataBind();
        }
    }

    public override void GetBreadCrumbs(BaseMasterPage.BreadCrumbCollection breadcrumbs)
    {
        base.GetBreadCrumbs(breadcrumbs);
        if (CurrentGroup != null)
        {
            breadcrumbs.Add(CurrentGroup.Name, string.Format("~/Topics/TopicGroup.aspx?GroupId={0}", CurrentGroup.Id));
        }
        if (CurrentTopic != null)
        {
            breadcrumbs.Add(CurrentTopic.Name, string.Format("~/Topics/TopicPosts.aspx?GroupId={0}&Id={1}", CurrentTopic.TopicGroupId, CurrentTopic.Id));
            breadcrumbs.Add("Posts", string.Format("~/Topics/TopicPosts.aspx?GroupId={0}&Id={1}", CurrentTopic.TopicGroupId, CurrentTopic.Id));
        }
    }

    protected void dsPosts_SelectData(object sender, Acms.DataControls.SelectEventArgs<Post> e)
    {
        DateTime? version = null;
        try
        {
            version = DateTime.Parse(tabSiteVersions.SelectedTab.Value);
        }
        catch
        {
        }
        e.SetData(Post.GetAllByTopicId(AcmsContext.SiteId, TopicId, version));
    }

    protected void tabSiteVersions_TabClick(object sender, Telerik.Web.UI.RadTabStripEventArgs e)
    {
        if (e.Tab.Selected)
        {
            winAdd.NavigateUrl = string.Format("PopUp/SelectPost.aspx?Id={1}&SV={0}", e.Tab.Value, TopicId);
            gridPosts.Rebind();
        }
    }
    protected void gridPosts_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.CommandName == "Remove")
        {
            GridDataItem dataItem = e.Item as GridDataItem;
            if(dataItem != null)
            {
                int id = (int)dataItem.GetDataKeyValue("Id");
                List<PostTopic> links = PostTopic.GetAllByPostId(id);
                foreach (PostTopic link in links)
                {
                    if (link.TopicId == base.TopicId)
                    {
                        link.Delete();
                        gridPosts.Rebind();
                        return;
                    }
                }
            }
        }
    }

    protected void lnkAdd_Click(object sender, EventArgs e)
    {
        gridPosts.Rebind();
    }

    protected override object SaveViewState()
    {
        ViewState["PopUpUrl"] = winAdd.NavigateUrl;
        return base.SaveViewState();
    }

    protected override void LoadViewState(object savedState)
    {
        base.LoadViewState(savedState);
        winAdd.NavigateUrl = ViewState["PopUpUrl"] as string;

    }
}
